package com.aistar.task;


import com.aistar.mapper.ProgramCollectMapper;
import com.aistar.pojo.ProgramCollect;
import com.aistar.pojo.ProgramCollectExample;
import com.aistar.util.MessageUtil;
import com.aistar.util.RedisMessage;
import com.aistar.util.RedisUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Configuration
@EnableScheduling   // 2.开启定时任务
public class ProgramRedisTask {
    private final ProgramCollectMapper programCollectMapper;

    private Log log = LogFactory.getLog(ProgramRedisTask.class);

    @Autowired
    public ProgramRedisTask(ProgramCollectMapper programCollectMapper) {
        this.programCollectMapper = programCollectMapper;
    }

    @Scheduled(cron = "5/5 * * * * ?") // 每5s运行一次
    private void runCollect() {
        int count = 0;
        // 将redis数据持久化到数据库中
        Map<Object, Object> map = RedisUtil.hgetAll(RedisMessage.PROGRAM_COLLECT_KEY);
        for (Map.Entry<Object, Object> entry : map.entrySet()) {
            String[] keys = ((String) entry.getKey()).split(RedisMessage.SEPARATOR);
            Integer customerId = Integer.valueOf(keys[0]);
            Integer programId = Integer.valueOf(keys[1]);

            String[] values = ((String) entry.getValue()).split(RedisMessage.SEPARATOR);
            int value = Integer.valueOf(values[0]);
            Date praiseTime = new Date(Long.valueOf(values[1]));

            ProgramCollectExample example = new ProgramCollectExample();
            example.createCriteria()
                    .andProgramCollectIdEqualTo(programId)
                    .andCustomerIdEqualTo(customerId);
            List<ProgramCollect> programCollects = programCollectMapper.selectByExample(example);
            ProgramCollect programCollect;
            if (programCollects != null && programCollects.size() > 0) { // 数据库中有数据
                if (programCollects.get(0).getStatus() != value) {
                    programCollect = programCollects.get(0);
                    programCollect.setStatus(value);
                    programCollect.setProgramCollectTime(praiseTime);
                    count += programCollectMapper.updateByPrimaryKeySelective(programCollect);
                }
            } else if (MessageUtil.STATUS_FAIL != value) { // 数据库中没有数据，需要新增
                programCollect = new ProgramCollect();
                programCollect.setCustomerId(customerId);
                programCollect.setProgramId(programId);
                programCollect.setProgramCollectTime(praiseTime);
                count += programCollectMapper.insertSelective(programCollect);
            }
            RedisUtil.hdel(RedisMessage.PROGRAM_COLLECT_KEY, (String) entry.getKey());
        }
        if (count != 0)
            log.info("redis programCollect change successful rows: " + count);
    }
}
